package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.*;
import com.sanjishu.enterlab.electricity.sale.mapper.*;
import com.sanjishu.enterlab.electricity.sale.utils.WordUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Service
public class PdfService {

    @Value("${path.root}")
    private String rootPath;
    @Value("${path.prefix.pdf}")
    private String pdfPath;
    @Value("${path.prefix.pdf.sim}")
    private String simPdfPath;
    @Autowired
    private ContractInfoMapper contractInfoMapper;
    @Autowired
    private CompanyInfoMapper companyInfoMapper;
    @Autowired
    private SysSettingService sysSettingService;
    @Autowired
    private ContractSettingMapper contractSettingMapper;
    @Autowired
    private ContractDataService contractDataService;
    @Autowired
    private ContractDataMapper contractDataMapper;
    @Autowired
    private ContractNumMapper contractNumMapper;
    @Autowired
    private ContractNumService contractNumService;


    @Transactional
    public Response generalContract(Integer id) throws Exception {
        ContractInfo contractInfo = contractInfoMapper.selectByPrimaryKey(id);
        ContractSetting contractSetting = contractSettingMapper.findByContractId(contractInfo.getId());
        if (contractInfo.getPlanType() == null) {
            return Response.getError("请先设置套餐类型!");
        }

        CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(contractInfo.getCompanyInfoId());
        int year = Calendar.getInstance().get(Calendar.YEAR);
        Map<String, String> map = new HashMap<>();
        map.put("$p1$", companyInfo.getName());
        map.put("$p2$", companyInfo.getAddress());
        map.put("$p3$", companyInfo.getBelongOrg());
        map.put("$p4$", companyInfo.getCreditCode());
        map.put("$p5$", companyInfo.getElecFor());
        map.put("$p6$", companyInfo.getOperName());
        if (contractInfo.getElecAccountNum() != null && !contractInfo.getElecAccountNum().equals("")) {
            map.put("$p7$", contractInfo.getElecAccountNum());
        } else {
            map.put("$p7$", "                                                            ");
        }

        if(StringUtils.isNotBlank(contractInfo.getBankAccountName())){
            map.put("$p8$", contractInfo.getBankAccountName());
        }else {
            map.put("$p8$", contractInfo.getName());
        }
        if (contractInfo.getBankName() != null && !contractInfo.getBankName().equals("")) {
            map.put("$p9$", contractInfo.getBankName());
        } else {
            map.put("$p9$", "                                  ");
        }
        if (contractInfo.getBankAccount() != null && !contractInfo.getBankAccount().equals("")) {
            map.put("$p10$", contractInfo.getBankAccount());
        } else {
            map.put("$p10$", "                                  ");
        }
        map.put("$p11$", contractInfo.getVoltageStep());
        if (contractInfo.getTransformerCapacity() != null && !contractInfo.getTransformerCapacity().equals("")) {
            map.put("$p12$", contractInfo.getTransformerCapacity());
        } else{
            map.put("$p12$", "                  ");
        }
        map.put("$p13$", (year+Integer.parseInt(contractInfo.getTradeCycle()))+"");
        map.put("$p14$", contractInfo.getElecConsumption());
        map.put("$p15$", (year+Integer.parseInt(contractInfo.getTradeCycle()))+"");
        ContractNum contractNum = contractNumMapper.findByContractId(contractInfo.getId());
        if (contractNum != null) {
            map.put("$p16$", contractNum.getNum());
        } else {
            String currentNum = sysSettingService.getNum();
            map.put("$p16$", currentNum);
            contractNumService.createContractNum(contractInfo.getId(), currentNum);
        }


        String filePath = pdfPath+System.currentTimeMillis()+".doc";
        String simPath = simPdfPath+System.currentTimeMillis()+".doc";
        if (contractInfo.getPlanType() == ContractInfo.PLAN_TYPE_1){
            map.put("$p17$", contractSetting.getLongAgreementRate()+"");
            map.put("$p18$", new BigDecimal(100).subtract(contractSetting.getLongAgreementRate())+"");
            WordUtils.createWord("word/fc.doc", rootPath+filePath, map);
            WordUtils.createWord("word/fc_jy.doc", rootPath+simPath, map);
        } else if (contractInfo.getPlanType() == ContractInfo.PLAN_TYPE_2){
            WordUtils.createWord("word/qf.doc", rootPath+filePath, map);
        } else if (contractInfo.getPlanType() == ContractInfo.PLAN_TYPE_3){
            if (contractSetting != null) {
                if (contractSetting.getCooperationType() == ContractSetting.TYPE_1) {
                    map.put("$p17$", contractSetting.getLongAgreementRate()+"");
                    map.put("$p18$", new BigDecimal(100).subtract(contractSetting.getLongAgreementRate())+"");
                    WordUtils.createWord("word/fc.doc", rootPath+filePath, map);
                    WordUtils.createWord("word/fc_jy.doc", rootPath+simPath, map);
                } else if (contractSetting.getCooperationType() == ContractSetting.TYPE_2) {
                    map.put("$p17$", contractSetting.getBottomPrice()+"");
                    WordUtils.createWord("word/bd.doc", rootPath+filePath, map);
                    WordUtils.createWord("word/bd_jy.doc", rootPath+simPath, map);
                }else if (contractSetting.getCooperationType() == ContractSetting.TYPE_4) {
                    map.put("$p17$", contractSetting.getDescription()+"");
                    WordUtils.createWord("word/zdy.doc", rootPath+filePath, map);
                    WordUtils.createWord("word/zdy_jy.doc", rootPath+simPath, map);
                }
            }
        }
        contractInfo.setPdfUrl(filePath);
        contractInfoMapper.updateByPrimaryKey(contractInfo);
        ContractData contractData = contractDataMapper.getByContractIdAndType(id, ContractData.PLAN_TYPE_5);
        if (contractData != null) {
            contractData.setFileUrl(filePath);
            contractDataMapper.updateByPrimaryKey(contractData);
        } else {
            contractData = contractDataService.createContractData(id, ContractData.PLAN_TYPE_5, filePath);
        }

        return Response.buildSuccess("合同生成成功。");
    }
}
